<?php
class WarehousesController extends AppController {
    var $name = 'Warehouses';

    var $pageTitle = 'Warehouse Information';

    var $viewPath = 'its/admins/warehouses';

    var $uses = array('Warehouse', 'User', 'RunningNo', 'Location', 'ProductSerial');

    function beforeFilter() {
        parent::beforeFilter();
        // ใช้ใน Tab menus element ระบุ Secondary controller ที่กำลังถูกเลือก
        $this->set('parentNav', 'its_admins');
        $this->set('tabMenu', 'tabmenus/its/admins');
    }

    function display() {
        parent::display();
        $this->_paging('Warehouse', 'LIKE');
    }

    function save() {
        $this->layout = 'ajax';
        $warehouse_id = null;
        if (!empty($this->data)) {
            $this->Warehouse->query("BEGIN");;
            $this->data['Warehouse']['collector_id'] = $this->Auth->user('id');
            if($this->Warehouse->save($this->data)) {
                $this->Warehouse->query("COMMIT");;
                $this->Session->setFlash("Warehouse {$this->data['Warehouse']['name']} save succesfull.", 'flash/ok');
                $warehouse_id = $this->Warehouse->id;
            } else {
                $this->Warehouse->query("ROLLBACK");
                $this->Session->setFlash("Warehouse save fail.", 'flash/fail');
                // ถ้าเป็น mode edit เก็บ id เอาไว้กรณีบันทึกข้อมูลผิด
                $warehouse_id = $this->data['Warehouse']['id'];
            }
        }
        $this->setAction('view', $warehouse_id);
    }

    function view($id = null) {
        if ($id != null) {
            $this->Warehouse->contain();
            $this->data = $this->Warehouse->findById($id);
            $this->Warehouse->id = $id;
            $this->set('neighbors', $this->Warehouse->find('neighbors', array('fields' => array('id', 'name'))));
            $this->set('mode', 'edit');
        } else {
            $this->set('mode', 'add');
        }
        $this->set('provinces', $this->Warehouse->Province->find('list'));
    }

    function delete($id = null) {
        $is_success = false;

        if($this->Warehouse->Location->findAllByWarehouseId($id) == null) {  // ถ้ายังไม่มี Location ให้ลบได้
            $is_success = $this->Warehouse->delete($id, true);
        }

        if ($is_success) { 
            $this->Session->setFlash("Warehouse delete succesfull.", 'flash/ok');
            $this->setAction('display');
        } else {
            $this->Session->setFlash("Can not delete this warehouse.", 'flash/fail');
            $this->setAction('view', $id);
        }
    }

    /**
     * action สำหรับ display location
     * @param int $warehouse_id
     */
    function display_locations($warehouse_id) {
        $this->layout = 'ajax';
        $this->Location->contain('User');
        $this->data = $this->Location->findAllByWarehouseId($warehouse_id);
        $this->set('warehouse_id', $warehouse_id);
    }

    function save_location() {
        $this->layout = 'ajax';
        $warehouse_id = $this->data['Location']['warehouse_id'];
        $this->Location->query("BEGIN");;
        $this->data['Location']['collector_id'] = $this->Auth->user('id');
        if($this->Location->save($this->data)) {
            $this->Location->query("COMMIT");;
            $this->Session->setFlash("Location {$this->data['Location']['name']} save succesfull.", 'flash/ok');
        } else {
            $this->Location->query("ROLLBACK");
            $this->Session->setFlash("Location save fail.", 'flash/fail');
        }
        $this->setAction('display_locations', $warehouse_id);
    }

    function delete_location($location_id) {
        $this->layout = 'ajax';
        $is_success = false;

        $this->Location->id = $location_id;
        $warehouse_id = $this->Location->field('warehouse_id');

        // ถ้า Location นี้ยังไม่เคยมี product อยุ่ลบได้
        if(!$this->ProductSerial->hasAny(array('location_id' => $location_id))) {
            $is_success = $this->Location->delete($location_id, true);
        }

        if ($is_success) {
            $this->Session->setFlash("Location delete succesfull.", 'flash/ok');
        } else {
            $this->Session->setFlash("Can not delete this location.", 'flash/fail');
        }
        $this->setAction('display_locations', $warehouse_id);
    }

    function location_inplace_editor($field, $id) {
        parent::inplace_editor($this->Location, $field, $id);
    }

    function option_location_list($warehouse_id) {
        $locations = $this->Location->find('list', array('conditions' => array('warehouse_id' => $warehouse_id)));
        parent::option_list($locations);
    }
}
?>
